package presentation.data;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTable;

import business.SessionCache;
import business.exceptions.BackendException;
import business.exceptions.UIException;
import business.externalinterfaces.CustomerConstants;
import business.externalinterfaces.CustomerSubsystem;
import business.externalinterfaces.Order;
import business.externalinterfaces.OrderItem;
import business.util.OrderUtil;
public class ViewOrdersData {
	public List<Order> getOrderHistory() {
		SessionCache context = SessionCache.getInstance();
        CustomerSubsystem customer = (CustomerSubsystem)context.get(CustomerConstants.CUSTOMER);
        if(customer == null) return new ArrayList<Order>();
        return customer.getOrderHistory();
	}
	/*
	public int getSelectedOrderId(JTable table) throws UIException {
		int selectedRow = table.getSelectedRow();
    	if(selectedRow >= 0) {
            String selOrderId = (String)table.getValueAt(selectedRow,0);
            Integer selOrderIdAsInt = Integer.parseInt(selOrderId);
            return selOrderIdAsInt;
    	} else {
    		throw new UIException(MessagesUtil.SELECT_ROW_MSG);
    	}
            
            
	}*/
	public Order getSelectedOrder(JTable table) throws UIException, BackendException {
		int selectedRow = table.getSelectedRow();
    	if(selectedRow >= 0) {
            String selOrderId = (String)table.getValueAt(selectedRow,0);
            //Integer selOrderIdAsInt = Integer.parseInt(selOrderId);
            Order selectedOrder = null;
            boolean orderFound = true;
            for(Order order : getOrderHistory()){
            	/*
                 if(order.getOrderId().equals(selOrderIdAsInt)){
                    selectedOrder = order;
                    orderFound = true;
                    break;
                }*/
            }
            if(!orderFound) {
            	throw new BackendException("Selected order not found!");
            	                                                   
                        
            } else {
            	return selectedOrder;
            }
    	} else {
    		throw new UIException(MessagesUtil.SELECT_ROW_MSG);
    	}
	}
	
	
	
	
	
	private static final ViewOrdersData instance = new ViewOrdersData();

	public static ViewOrdersData getInstance() {
		return instance;
	}
}
